import 'dart:async';
import 'dart:developer';

import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/services.dart';
import 'package:fluwx/fluwx.dart';
import 'package:get/get.dart';
import 'package:logger/logger.dart';
import 'package:logger/web.dart';
import 'package:sports_new/controller/expert/plan_logic.dart';
import 'package:sports_new/controller/home/home_logic.dart';
import 'package:sports_new/controller/match/list/match_page_logic.dart';
import 'package:sports_new/controller/my/my_page_logic.dart';
import 'package:sports_new/controller/navigation_logic.dart';
import 'package:sports_new/controller/record/data_logic.dart';
import 'package:sports_new/controller/service/push_service.dart';
import 'package:sports_new/controller/service/resource_service.dart';
import 'package:sports_new/controller/service/umeng_service.dart';
import '../../server/http/request_header.dart';
import '../../utils/ali_message.dart';
import '../../utils/app_config.dart';
import '../../utils/mode_config.dart';
import '../../utils/online_contact.dart';
import '../../utils/rsa_util.dart';
import '../../utils/sp_utils.dart';

class ConnectService extends GetxService {
  bool hasNetwork = true;
  bool enterLost = false;
  StreamSubscription? _subscription;

  Future<ConnectService> init() async {
    List<ConnectivityResult> result = await Connectivity().checkConnectivity();
    if (result.contains(ConnectivityResult.none)) {
      hasNetwork = false;
      enterLost = true;
    }
    return this;
  }

  StreamSubscription connectListener() {
    return Connectivity().onConnectivityChanged.listen((event) async {
      if (event.contains(ConnectivityResult.none)) {
        hasNetwork = false;
        reconnect();
      } else if (hasNetwork == false) {
        hasNetwork = true;
        if (enterLost == true) {
          await start();
        }
        reconnect();
      }
      enterLost == false;
    });
  }

  @override
  void onInit() {
    _subscription = connectListener();
    super.onInit();
  }

  Future start() async {
    await AppConfig.init();
    await SpUtils.initSp();
    await RequestHeader.preInit();
    await RsaUtil.init();
    await AppConfig.config.getLimit();
    await Get.find<ResourceService>().getAppLaunchConfig();
    await afterPrivacy();
  }

  static Future afterPrivacy() async {
    if (SpUtils.agreePrivacy == true) {
      await RequestHeader.init();
      await Get.find<ResourceService>().getResource();
      OnlineContact().init();
      ModeConfig.init();
      Get.find<UmengService>().initUm();
      try {
        const MethodChannel("mainInit").invokeMethod("initAds");
      } catch (e) {
        Logger().d(e);
      }
      await Get.find<PushService>().initPush();
      await Get.find<ResourceService>().getAppStartConfig();
      Get.find<NavigationLogic>().getRefresh();
      AliMessage().login();
    }
  }

  Future reconnect() async {
    try {
      Get.find<HomeLogic>().getRefresh();
    } catch (e) {
      log("HomeLogic not start");
    }
    try {
      Get.find<MatchPageLogic>().getRefresh();
    } catch (e) {
      log("MatchPageLogic not start");
    }
    try {
      Get.find<PlanLogic>().getRefresh();
    } catch (e) {
      log("PlanLogic not start");
    }
    try {
      Get.find<DataLogic>().getRefresh();
    } catch (e) {
      log("DataLogic not start");
    }
    try {
      Get.find<MyPageLogic>().getRefresh();
    } catch (e) {
      log("MyPageLogic not start");
    }
  }

  void startListen() {
    if (_subscription != null) return;
    Logger().d("无网监听开启");
    _subscription = connectListener();
  }

  void stopListen() {
    if (_subscription != null) {
      Logger().d("无网监听关闭");
      _subscription?.cancel();
      _subscription = null;
    }
  }
}
